Ext.define('CDR.controller.TariffController', {
    extend: 'Ext.app.Controller',
    views: [
        'TariffSetting',
        'TariffConfig',
        'TariffGroupConfig',
        'TariffAreaCodeConfig',
        'TariffCallRecord',
        'TariffAMQ',
        'TariffConsoleLogAMQ',
        'TariffCallLogAMQ'
        
    ],
   stores: [
    	'TariffTreeStore'    
    ],
    models: [
    	'TariffTreeModel',
    	'LocalStoreModel',
    	'CallLogModel'
    ],
    onLaunch:function(){
		
	},
    init: function() {
    	//create local store
		CDRUtils.localStore = Ext.create('Ext.data.Store', {
		    model: "CDR.model.LocalStoreModel"
		});
		CDRUtils.localStore.load();
		console.log("CDRUtils.localStore", CDRUtils.localStore);
	  	
        this.control({
           'tariff-setting toolbar button[name=new-tf]' : {
	           	click: function(){
	           		CDRUtils.createTariffNode();
	           	}
           },
           
           'tariff-setting toolbar button[name=new-group]' : {
	           	click: function(){
	           		CDRUtils.createGroupNode();
	           	}
           },
           
           'tariff-setting toolbar button[name=new-code]' : {
	           	click: function(){
	           		CDRUtils.createAreaNode();
	           	}
           },
           
           'tariff-setting toolbar button[name=delete]' : {
	           	click: function(cmp){
	           		var node = CDRUtils.getTariffSelectedNode();
	           		if(node == null) {
	           			Ext.Msg.alert('Delete', 'Please select item');
	           		} else if(node.childNodes.length > 0){
	           			Ext.Msg.alert('Delete', 'The item has children so you can not delete');
	           		} else {
	           			Ext.Msg.confirm('Delete','Are you sure you want to delete this one',function(btn){
							if (btn=='yes') {
								switch(node.data.type){
			           				case CDRUtils.TariffType.TARIFF: 
			           					CDRUtils.deleteTariffNode('deleteTariffRecord', {
			           						id: node.data.id
			           					}, function(){
			           						CDRUtils.reloadTariffTree();
			           					});
			           					break;
			           				case CDRUtils.TariffType.GROUP:
			           					CDRUtils.deleteTariffNode('deleteTariffGroup', {
			           						id: node.data.obId
			           					}, function(){
			           						CDRUtils.reloadTariffTree();
			           					});
			           					break;
			           				case CDRUtils.TariffType.AREACODE:
			           					CDRUtils.deleteTariffNode('deleteTariffAreaCode', {
			           						id: node.data.obId
			           					}, function(){
			           						CDRUtils.reloadTariffTree();
			           					});
			           					break;
			           			}
							}
						});
	           			
	           		}
	           	}
           },
           'tariff-setting toolbar button[name=refresh]' : {
	           	click: function(cmp){
	           		var activeItem = cmp.up('window').down('toolbar cycle').activeItem;
	           		var rf = cmp.up('window').el;
	           		rf.mask('Loading ...');
	           		Ext.Ajax.request({
						url: 'reloadConfig',
						params: {
							tariffId : activeItem.id
						},
						success: function(response){
							rf.unmask();
							var data = Ext.decode(response.responseText);
							if(data.success) {
								Ext.Msg.alert('Info', 'Reload config done');
							}
						},
						failure:function(){
							rf.unmask();
							Ext.Msg.alert('Error', 'Reload config fails');
						}
					});
	           	}
           },
           'tariff-setting panel toolbar cycle' : {
	           	change: function(comp, item){
	           		CDRUtils.initTariffTree(item.id);
	           	}
           }, 
           
           'tariff-setting tariff-config button[name=save]' : {
	           	click: function(comp){
	           		console.log(">>>>>>>>>>>>>>>>>submit data", comp.up('form'));
	           		var view = comp.up('tariff-config');
	           		var formPanel = comp.up('form');
	           		var form = formPanel.getForm();
	           		
	           		var data = view.buildExtraData();
	           		console.log(data);
	           		if(form.isValid()) {
	           			form.submit({
	           				params: data,
	           				url: 'saveTariffNode',
	           				success: function(){
	           					Ext.Msg.alert('Save', 'Save is ok');
	           					CDRUtils.reloadTariffTree();
	           				}, 
	           				failure:  function() {
	           					Ext.Msg.alert('Save', 'Save is fail');
	           				}
	           			});
	           		}
	           	}
           },
           
           'tariff-setting tariff-group-config button[name=save]' : {
	           	click: function(comp){
	           		console.log(">>>>>>>>>>>>>>>>>submit data", comp.up('form'));
	           		var view = comp.up('tariff-group-config');
	           		var formPanel = comp.up('form');
	           		var form = formPanel.getForm();
	           		
	           		var data = view.buildExtraData();
	           		console.log(data);
	           		if(form.isValid()) {
	           			form.submit({
	           				params: data,
	           				url: 'saveGroupNode',
	           				success: function(){
	           					Ext.Msg.alert('Save', 'Save is ok');
	           					CDRUtils.reloadTariffTree();
	           				}, 
	           				failure:  function() {
	           					Ext.Msg.alert('Save', 'Save is fail');
	           				}
	           			});
	           		}
	           	}
           },
           
           
           
           'tariff-setting tariff-areacode-config button[name=save]' : {
	           	click: function(comp){
	           		console.log(">>>>>>>>>>>>>>>>>submit data", comp.up('form'));
	           		var view = comp.up('tariff-areacode-config');
	           		var formPanel = comp.up('form');
	           		var form = formPanel.getForm();
	           		
	           		var data = view.buildExtraData();
	           		console.log(data);
	           		if(form.isValid()) {
	           			form.submit({
	           				params: data,
	           				url: 'saveAreaNode',
	           				success: function(){
	           					Ext.Msg.alert('Save', 'Save is ok');
	           					CDRUtils.reloadTariffTree();
	           				}, 
	           				failure:  function() {
	           					Ext.Msg.alert('Save', 'Save is fail');
	           				}
	           			});
	           		}
	           	}
           },
           'tariff-setting treepanel': {
				itemclick:function(treeView, item){
					console.log(">>>>>>>>>", item);
					var settingPanel = treeView.up('tariff-setting');
					//check and enable button
					if(item.data.type === CDRUtils.TariffType.TARIFF) {
						Ext.getCmp('x-cdr-tariff-setting-new-group').enable();
						Ext.getCmp('x-cdr-tariff-setting-new-code').disable();
						settingPanel.maskView();
						CDRUtils.loadTariffNode('loadTariffRecord', {
							id: item.data.obId
						}, function(data){
							//change view
							if(data.success){
								settingPanel.changeView('tariff-config', data.result);
							}
							settingPanel.unMaskView();
						});
					} else if (item.data.type === CDRUtils.TariffType.GROUP) {
						Ext.getCmp('x-cdr-tariff-setting-new-code').enable();
						Ext.getCmp('x-cdr-tariff-setting-new-group').disable();
						settingPanel.maskView();
						CDRUtils.loadTariffNode('loadTariffGroup', {
							id: item.data.obId
						}, function(data){
							//change view
							if(data.success){
								settingPanel.changeView('tariff-group-config', data.result);
							}
							settingPanel.unMaskView();
						});
					} else {
						Ext.getCmp('x-cdr-tariff-setting-new-group').disable();
						Ext.getCmp('x-cdr-tariff-setting-new-code').disable();
						settingPanel.maskView();
						CDRUtils.loadTariffNode('loadTariffAreaCode', {
							id: item.data.obId
						}, function(data){
							//change view
							if(data.success){
								settingPanel.changeView('tariff-areacode-config', data.result);
							}
							settingPanel.unMaskView();
						});
					}
				},
				itemcontextmenu:function(tree,record,dom,idx,event){
					event.preventDefault();
					event.stopEvent();
					if(record.data.id == 'root'){
						return;
					}
					var menu = new Ext.menu.Menu({
						floating:true,
						items: [{
							text: 'Clone node',
							handler: function(){
								CDRUtils.cloneAreaNode();
							}
						}]
					});
					menu.showAt(event.getXY());
				},
				
				itemexpand: function(node){
					//CDRUtils.localStore.add({expandId: node.data.obId});
					//CDRUtils.localStore.sync();
				},
				
				itemcollapse: function(node){
					//console.log('index', CDRUtils.localStore.findExact('expandId', node.data.obId), CDRUtils.localStore);
					//CDRUtils.localStore.removeAt(CDRUtils.localStore.findExact('expandId', node.data.obId));
				}
           },
           
           'tariff-setting tariff-config gridpanel toolbar button[name=add-date]' : {
	           	click: function(bnt){
	           		var grid = bnt.up('gridpanel');
	           		if(grid){
	           			grid.getStore().add({
							date: new Date(),
							dateType: 'DL',
							description: 'Input description here'	           				
	           			});
	           		}
	           	}
           },
           
           'tariff-setting tariff-group-config gridpanel toolbar button[name=add-new]' : {
	           	click: function(bnt){
	           		var grid = bnt.up('gridpanel');
	           		if(grid){
	           			grid.getStore().add({
							starttime: new Date(),
							endtime: new Date(),
							discount: 0	           				
	           			});
	           		}
	           	}
           },
           
           'tariff-setting tariff-areacode-config gridpanel toolbar button[name=add-new]' : {
	           	click: function(bnt){
	           		var grid = bnt.up('gridpanel');
	           		if(grid){
	           			grid.getStore().add({
							effectedDate: new Date(),
							fristBlock: 0,
							secondBlock: 0
	           			});
	           		}
	           	}
           },
           
           'tariff-call-record toolbar button[name=tab-view]' : {
	           	click: function(comp){
	           		comp.up('tariff-call-record').changeView(0);
	           	}
           },
           
           'tariff-call-record toolbar button[name=horizontal-view]' : {
	           	click: function(comp){
	           		comp.up('tariff-call-record').changeView(1);
	           	}
           },
           
           'tariff-call-record toolbar button[name=vertical-view]' : {
	           	click: function(comp){
	           		comp.up('tariff-call-record').changeView(2);
	           	}
           },
        });
    }

});